package com.ll.menu.user.welcome;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.app.adapter.BaseAbstractAdapter;
import com.ll.menu.user.R;

import java.util.ArrayList;


/**
 * DropdownPopupWindow
 *
 * @author wangheng
 */
public class CountPopupWindow extends PopupWindow {

    private ListView mListView;

    public CountPopupWindow(Context context,int width){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popup_count,null,false);


        // 设置SelectPicPopupWindow的View
        setContentView(view);

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);

        // 点back键和其他地方使其消失,设置了这个才能触发OnDismissListener ，设置其他控件变化等操作
        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupDropdown);

        initListView(context,view);

    }

    private static final int MAX_COUNT = 12;
    private ArrayList<Integer> mList = new ArrayList<>(MAX_COUNT);

    private void initListView(Context context, View view) {
        mListView = (ListView) view.findViewById(R.id.lvPopupCount);
        for(int i = 0;i < MAX_COUNT;i++){
            mList.add(i + 1);
        }
        Adapter adapter = new Adapter(context);
        adapter.resetDataList(mList);
        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mListener != null){
                    mListener.onItemSelect(mList.get(position));
                }
                dismiss();
            }
        });
    }

    private OnItemSelectListener mListener;

    public interface OnItemSelectListener{
        void onItemSelect(int count);
    }

    public void setOnItemSelectListener(OnItemSelectListener listener){
        mListener = listener;
    }

    private class Adapter extends BaseAbstractAdapter<Integer>{

        private String mFormatter = "";

        Adapter(Context context){
            super(context);
            mFormatter = App.getInstance().getString(R.string.people_count_mode);
        }
        @Override
        protected int getLayoutId() {
            return R.layout.item_popup_count;
        }

        @Override
        protected void inflate(View convertView, Integer integer, int position) {
            TextView textView = (TextView) findViewById(R.id.tvPopupCountItem);
            textView.setText(String.format(mFormatter,integer));
        }
    }
}
